package com.mhxy;

import com.mhxy.controller.StartController;
import com.mhxy.util.DirUtil;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.net.URL;

@Slf4j
public class MainApp extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            String currentDir = System.getProperty("user.dir");
            System.out.println("currentDir: " + currentDir);
            URL url = getClass().getClassLoader().getResource("mhxyMain.fxml");
            System.out.println("url path: " + url.getPath());
            System.out.println("jar: " + DirUtil.getJarDirectory());
            System.out.println("gamePic path: " + getClass().getClassLoader().getResource("gamePic"));

            // 加载.fxml文件
            FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("mhxyMain.fxml"));
            Parent root = loader.load();

            // 创建场景并设置到舞台
            Scene scene = new Scene(root, 730, 580);
            primaryStage.setTitle("PC任务助手v1.0");
            primaryStage.setScene(scene);
            primaryStage.show();

            // 添加一个事件处理程序，在关闭窗口时弹出警告框
            primaryStage.setOnCloseRequest(event -> {
                // 关闭游戏线程
                StartController.executorService.shutdownNow();
            });
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to load FXML file", e);
        }
    }

    public static void main(String[] args) {
        Application.launch(MainApp.class, args);
    }

}
